package test_activiti;

import java.util.List;
import java.util.Map;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

public class ActivitiUtils {

	public static void printTaskLocalVari(Task task){
		System.out.println("==打印本地变量 "+task.getId());
	    Map<String,Object> localM = task.getTaskLocalVariables();
	    for(Map.Entry<String,Object> et:localM.entrySet()){
	    	System.out.println("\t"+et.getKey()+"="+et.getValue().toString());
	    }
	    System.out.println("==打印本地变量 结束");
	}

	/**
	 * 查询流程的历史变量等信息。注意，需要设置 <property name="history" value="full"/>
	 * @param historicDataService
	 * @param runtimeService
	 * @param piId
	 */
	public static void printProcInstHistory(HistoryService historicDataService,RuntimeService runtimeService ,String piId){
		ProcessInstance pi=null;
	    System.out.println("====查询历史====");
	    List<HistoricTaskInstance>  list = historicDataService.createHistoricTaskInstanceQuery()
	    .processInstanceId(piId)
	    .orderByHistoricTaskInstanceEndTime().asc().list();
	    for(HistoricTaskInstance item:list){
	    	System.out.print("Assignee:"+item.getAssignee()
	    	+"\tid:"+item.getTaskDefinitionKey()
	    	+"\tname:"
	    +item.getName()+"\tcreateTime:"+item.getCreateTime()
	    +"\twtime:"+item.getWorkTimeInMillis()
	    +"\tendTime:"+item.getEndTime()
	    +"\texeId:"+item.getExecutionId()
	    +"\tdt:"+item.getDurationInMillis()
	    			);
	    	System.out.println("");
	    }

	    List<HistoricVariableInstance> listv = historicDataService.createHistoricVariableInstanceQuery().processInstanceId(piId)
	    		.list();
	    for(HistoricVariableInstance variable : listv) {
	     System.out.println("\t\tvariable:"
	     + variable.getVariableName()
	     + "= " + variable.getValue()
	     +"\ttaskid="+variable.getTaskId() //如果是task级别的变量才有，不然为null
	     +"\ttype="+variable.getVariableTypeName() //string 或serializable等等
	     +"\tprocId="+variable.getProcessInstanceId()); //总是有
	    }

	    pi = runtimeService.createProcessInstanceQuery().processInstanceId(piId).singleResult();
	    if(pi!=null)
	    	System.out.println("isEnd:"+pi.isEnded());
	    else
	    	System.out.println("isEnd:"+true);
	  }
}
